package xjtu.sad.zbsj.statistics;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xjtu.sad.zbsj.common.Result;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/statistics")
public class StatisticsAPI {

    @Resource
    private StatisticsService statisticsService;

    @RequestMapping("/withdraw")
    public Result withdrawMoney(@RequestBody  StatisticsDto statisticsDto){
        if(Float.valueOf(statisticsDto.getMoney())>=0){
            if(Float.valueOf(statisticsService.getCredit(statisticsDto.getUser_id()))<Float.valueOf(statisticsDto.getMoney())){
                return Result.fail(Result.ERR_CODE_UNWITHDRAW,"积分不足");
            }
            else{
                statisticsService.withdrawMoney(statisticsDto);
                return Result.success("success");
            }
        }
        else{
            return Result.fail(Result.ERR_CODE_UNWITHDRAW,"提取积分不能为负数");
        }

    }

    @RequestMapping("/withdrawHistory")
    public Result getHistory(String user_id){
        List<WithdrawHistory> historys = statisticsService.getHistory(user_id);
        return Result.successData(historys);
    }

    @RequestMapping("/credit")
    public Result getCredit( String user_id){
        System.out.println(statisticsService.getCredit(user_id));
        return Result.successData(statisticsService.getCredit(user_id));
    }

    @RequestMapping("/tasknums")
    public Result getFinishNum(String user_id,String year){
        MonthNum nums = statisticsService.selectTaskNum(user_id,year);
        return Result.successData(nums);
    }

    @RequestMapping("/tasknums2")
    public Result getStartNum(String user_id,String year){
        MonthNum nums = statisticsService.selectTaskSNum(user_id,year);
        return Result.successData(nums);
    }

    @RequestMapping("/rightrate")
    public Result getRightRate(String user_id){
        List<Float> rates= statisticsService.selectRightRate(user_id);
        return Result.successData(rates);
    }

    @RequestMapping("/totalrate")
    public Result getTotalRightRate(String user_id){
        float rate= statisticsService.selectTotalRightRate(user_id);
        return Result.successData(rate);
    }

    @RequestMapping("/finishnum")
    public Result getCount(String user_id){
        int count= statisticsService.getCount(user_id);
        return Result.successData(count);
    }
}
